package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @RequestMapping("list")
    @ResponseBody
    public List<Role> list() {
        return roleService.selectAll();
    }

    @RequestMapping("view")
    @RequiresPermissions(value = {"role:view", "角色界面"}, logical = Logical.OR)
    public String query(QueryObject qo) {
        return "role";
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"role:saveOrUpdate", "角色新增/更新"}, logical = Logical.OR)
    @ResponseBody
    public Object saveOrUpdate(Role role) {
        JSONResult result = new JSONResult();
        try {
            roleService.saveOrUpdate(role);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult role(QueryObject qo) {
        return roleService.query(qo);
    }

    @RequestMapping("validateSn")
    @ResponseBody
    public Boolean validateName(String sn, Long id) {
        return roleService.validateSn(sn, id);
    }

    @RequestMapping("selectByEmployeeId")
    @ResponseBody
    public List<Long> selectByEmployeeId(Long employeeId) {
        return roleService.selectByEmployeeId(employeeId);
    }

    @RequestMapping("remove")
    @ResponseBody
    public JSONResult remove(Long id) {
        JSONResult result = new JSONResult();
        try {
            roleService.remove(id);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
